#pragma once

#include <list>
#include "Vector2D.h"
#include "GraphEdge.h"
#include "GraphTypes.h"
#include "SearchAlgorithms.h"
#include "PathEdge.h"

using namespace std;

/*! Path
	=================================================
	The path class holds information on a path that 
	a game entity is following. It holds a list of
	PathEdges that the entity is to follow to traverse
	this path. */

typedef SparseGraph<NavGraphNode<void*>, GraphEdge> NavGraph;
class Path
{
public:



	//! Holds all the edges for the path
	list<PathEdge> m_EdgeList;
	
	//! The next waypoint the entity must reach.
	Vector2D curWaypoint;

	//! The path edge the entity is currently traversing. 
	list<PathEdge>::iterator curEdge;

	int FindClosestNodeToPosition(Vector2D Pos);

public:

	Path(Vector2D start, Vector2D end, Permissions perm = OVER_LAND);
	Path(NavGraph& graph, int start, int finish, bool noisy = false);

	Path(const Path& CopyPath);
	Path(const list<PathEdge> edge_list);


	Vector2D CurrentWaypoint() const{return curWaypoint;}

	bool Finished(){return curWaypoint == Vector2D();}
	bool IsValid(){return !m_EdgeList.empty();}
	Vector2D lastPoint() {return m_EdgeList.back().Destination();}

	list<PathEdge> GetPath()const{return m_EdgeList;}
	void SetPath(list<PathEdge> path) {m_EdgeList = path; curWaypoint = m_EdgeList.front().Source();}

	list<Vector2D> GetWaypoints();
	
	inline void SetToNextWaypoint();
	void Draw();
};

inline void Path::SetToNextWaypoint()
{
	if(curEdge != (--m_EdgeList.end()) && curEdge != m_EdgeList.end())
	{
		curEdge++;
		curWaypoint = curEdge->Source();
	}
	else if(curEdge == (--m_EdgeList.end()))
	{
		
		curWaypoint = curEdge->Destination();
		curEdge++;
	}
	else
	{
		curWaypoint = Vector2D();
	}
		
}